package com.gpy.hospital.cms.controller;

import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.pojo.Blog;
import com.gpy.hospital.cms.service.IBlogService;
import com.gpy.hospital.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class BlogController {
    @Autowired
    private IBlogService blogService;

    @GetMapping("/page/blog")
    public String blogPage() {
        return "/cms/blogList";
    }

    /**
     * 跳转到博客界面
     */
    @RequestMapping("/blog/data")
    @ResponseBody
    public ServerResponse blogPage(@RequestParam(value = "blogName", required = false) String blogName,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<Blog> blogs = blogService.getAll(pageNum, pageSize, blogName);
        return ServerResponse.createBySuccess("查询成功！", blogs.getList(), blogs.getTotal());
    }

    @GetMapping("/cms/blogEdit")
    public String blogEdit(@RequestParam("blogId") Integer blogId, Model model) {
        Blog blog = blogService.getById(blogId);
        model.addAttribute("blog", blog);
        return "/cms/blogEditPage";
    }

    /**
     * 添加文章
     */
    @RequestMapping("page/addBlog")
    @ResponseBody
    public ServerResponse addArticle(Blog blog) {
        return blogService.addArticle(blog);
    }

    /**
     * 添加页面
     *
     * @return
     */
    @GetMapping("/cms/blog/add")
    public String blogAdd() {
        return "/cms/blogAdd";
    }

    /**
     * 文章详情
     */
    @GetMapping("/blog/detail")
    public String blogDetail(@RequestParam("blogId") Integer blogId, Model model) {
        Blog byId = blogService.getById(blogId);
        model.addAttribute("blog", byId);
        return "/reception/blogDetail";
    }

    /**
     * 删除文章
     */
    @RequestMapping("/cms/blog/delete")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("blogId") Integer blogId) {
        return blogService.deleteById(blogId);
    }


    @GetMapping("/reception/blog/list")
    public String receptionBlogList(Model model) {
        List<Blog> blogList =  blogService.getAllByStatus();
        model.addAttribute("blogList", blogList);
        model.addAttribute("size", blogList.size());
        return "/reception/blogList";
    }

}
